package com.leetcode.two_pointers;

import java.util.Arrays;
import java.util.HashSet;

/**
 * @author Dennis Li
 * @date 2020/7/18 16:20
 */
public class ReverseVowels_345 {

    private final static HashSet<Character> vowels = new HashSet<>(
            Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));

    char[] array;

    public String reverseVowels(String s) {
        array = s.toCharArray();
        for (int i = 0, j = s.length() - 1; i < j; ) {
            boolean ca = vowels.contains(array[i]), cb = vowels.contains(array[j]);
            if (ca && cb) {
                swap(i, j);
                i++;
                j--;
            } else if (ca) {
                j--;
            } else if (cb) {
                i++;
            } else {
                i++;
                j--;
            }
        }
        return String.valueOf(array);
    }

    private void swap(int x, int y) {
        char temp = array[x];
        array[x] = array[y];
        array[y] = temp;
    }

}
